package util;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;

public class HttpUtil {
    public static final String CONTENT_TYPE = "Content-Type";
    public static final String X_WWW_FORM_URLENCODED = "application/x-www-form-urlencoded";
    public static final String JSON = "application/json";


    public static final String CHARSET = "charSet";
    public static final String UTF_8 = "UTF-8";
    public static final String GBK = "GBK";


    public static String post(String url, Map<String, String> header, Map<String, String> body) throws Exception {

        Map<String, String> defaultHeader = new HashMap();
//        defaultHeader.put("CONTENT_TYPE", X_WWW_FORM_URLENCODED);
//        defaultHeader.put("CHARSET", UTF_8);


        //1.构造HttpClient的实例
        HttpClient httpClient = new HttpClient();
        httpClient.getParams().setContentCharset("UTF-8");
        //2.构造PostMethod的实例
        PostMethod postMethod = new PostMethod(url);
        //3.把参数值放入到PostMethod对象中
        if (body != null) {
            for (Map.Entry<String, String> entry : body.entrySet()) {
                if (entry.getKey() == null || entry.getValue() == null) {
                    continue;
                }
                postMethod.addParameter(entry.getKey(), entry.getValue().toString());
            }
        }

        if (header != null) {
            for (Map.Entry<String, String> entry : defaultHeader.entrySet()) {
                if (!header.containsKey(entry.getKey())) {
                    header.put(entry.getKey(), entry.getValue());
                }
            }
        } else {
            header = defaultHeader;
        }

        for (Map.Entry<String, String> entry : header.entrySet()) {
            if (entry.getKey() == null || entry.getValue() == null) {
                continue;
            }
            postMethod.addRequestHeader(entry.getKey(), entry.getValue().toString());
        }

        try {
            // 4.执行postMethod,调用http接口
            httpClient.executeMethod(postMethod);
            return new String(postMethod.getResponseBody(), "UTF-8");
        } finally {
            //7.释放连接
            postMethod.releaseConnection();
        }

    }

    public static void main(String args[]) throws Exception {


//        float s0 = Float.NaN;
//        System.out.println(Float.NaN>Float.NaN);
//        System.out.println(Float.NaN<Float.NaN);
//        System.out.println(Float.NaN==Float.NaN);
//        System.out.println(2.00-1.10==0.9);

//        float a=0.89999999F;
//        float b=0.9f;
//        System.out.println(a==b);
//        DecimalFormat df = new DecimalFormat("0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000");
//
//
//        System.out.println(df.format(a));
//        System.out.println(df.format(b));
//        System.out.println(Float.toString(b));
//        System.out.println(Integer.toBinaryString(Float.floatToRawIntBits(-1f)));

//        0 0111 1110 1100 1100 1100 1100 1100 110
        //0011 1111 1000 0000 0000 0000 0000 0000
        //1011 1111 1000 0000 0000 0000 0000 0000


        double a=2-1.1;
        double b=0.9;
        System.out.println(0==Float.MIN_VALUE);
        DecimalFormat df = new DecimalFormat("0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000");


        System.out.println(df.format(a));
        System.out.println(df.format(b));
        System.out.println(Double.toString(b));
        System.out.println(Integer.toBinaryString(Float.floatToRawIntBits(-1f)));

        System.out.println(Float.compare(-0f,0f));
        System.out.println(Float.valueOf(-0f).equals(0f));






    }


}

//
//class Header{
//    Map<String,String> data=new HashMap<String, String>();
//
//
//
//}
